//
//  OrderPController.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/12/11.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "OrderPController.h"
#import "OrderTextField.h"
#import <AlibcTradeSDK/AlibcTradeSDK.h>
#import "JDKeplerManager.h"

@interface OrderPController ()
@property (nonatomic, weak) UIScrollView *scrollV;
@property (nonatomic, weak) OrderTextField *orderNum;
@property (nonatomic, weak) OrderTextField *orderName;
@property (nonatomic, weak) OrderTextField *orderPrice;
@end

@implementation OrderPController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WhiteColor;
    
    UIScrollView *saacrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 5, self.view.size.width, HEIGHT-100)];
    saacrollView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    saacrollView.showsHorizontalScrollIndicator = NO;
    saacrollView.showsVerticalScrollIndicator = NO;
    saacrollView.contentSize = CGSizeMake(WIDTH, 1370);
    saacrollView.scrollEnabled = YES;
    saacrollView.userInteractionEnabled = YES;
    [self.view addSubview:saacrollView];
    self.scrollV = saacrollView;
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, self.view.size.width-20, 350)];
    topView.backgroundColor = [UIColor whiteColor];
    topView.userInteractionEnabled = YES;
    [saacrollView addSubview:topView];

    MKViewBorderRadius(topView, 10, 0, WhiteColor);
    
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(10, CGRectGetHeight(topView.frame)+20, self.view.size.width-20, 925)];
    imgV.image = self.IndexType == 1 ? [UIImage imageNamed:@"taobaozhinan"] : [UIImage imageNamed:@"jingdongzhinan"];
    imgV.userInteractionEnabled = YES;
    [saacrollView addSubview:imgV];
    
    UILabel *orderLabel = [[UILabel alloc] init];
    orderLabel.text = @"输入订单编号匹配订单信息";
    orderLabel.textAlignment = NSTextAlignmentCenter;
    orderLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    [topView addSubview:orderLabel];
    [orderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(topView.mas_centerX);
        make.top.mas_equalTo(20);
    }];
    
    OrderTextField *textField = [[OrderTextField alloc] init];
    textField.placeholder = @"点击输入订单编号";
    textField.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    textField.textAlignment = 0;
    [topView addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(orderLabel.mas_bottom).offset(20);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(40);
    }];
    MKViewBorderRadius(textField, 10, 0, WhiteColor);
    self.orderNum = textField;
    
    OrderTextField *textFieldName = [[OrderTextField alloc] init];
    textFieldName.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    textFieldName.placeholder = @"点击输入订单商品名称";
    textFieldName.textAlignment = 0;
    [topView addSubview:textFieldName];
    [textFieldName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textField.mas_bottom).offset(10);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(40);
    }];
    MKViewBorderRadius(textFieldName, 10, 0, WhiteColor);
    
    OrderTextField *moeyTextField = [[OrderTextField alloc] init];
    moeyTextField.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    moeyTextField.placeholder = @"点击输入订单金额";
    moeyTextField.textAlignment = 0;
    [topView addSubview:moeyTextField];
    [moeyTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textFieldName.mas_bottom).offset(10);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(40);
    }];
    MKViewBorderRadius(moeyTextField, 10, 0, WhiteColor);
    
    self.orderName = textFieldName;
    self.orderPrice = moeyTextField;
    
    UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureButton setTitle:@"确认匹配" forState:UIControlStateNormal];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [sureButton setTitleColor:WhiteColor forState:UIControlStateNormal];
    [sureButton setBackgroundImage:[UIImage imageNamed:@"surepipei"] forState:UIControlStateNormal];
    [topView addSubview:sureButton];
    [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(topView.mas_centerX);
        make.top.equalTo(moeyTextField.mas_bottom).offset(30);
        make.size.mas_equalTo(CGSizeMake(215, 40));
    }];
    MKViewBorderRadius(sureButton, 20, 0, WhiteColor);
    
    [sureButton addTarget:self action:@selector(sureButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *gotaoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [gotaoButton setTitleColor:[UIColor colorWithHexString:@"#eb3223"] forState:UIControlStateNormal];
    [gotaoButton setTitle:self.IndexType == 1 ? @"去淘宝订单" : @"去京东订单" forState:UIControlStateNormal];
    gotaoButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [topView addSubview:gotaoButton];
    [gotaoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(topView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(200, 40));
        make.top.equalTo(sureButton.mas_bottom).offset(30);
    }];
    MKViewBorderRadius(gotaoButton, 20, 1, [UIColor colorWithHexString:@"#eb3223"]);
    
    [gotaoButton addTarget:self action:@selector(goToTaoOrderClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.scrollV.contentSize = CGSizeMake(WIDTH, 1370);
    });

}


- (void)sureButtonClick{

    if (self.orderNum.text.length < 1) {
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入订单编号"];
        return;
    }
    if (self.orderName.text.length < 1) {
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入订单商品名称"];
        return;
    }
    if (self.orderPrice.text.length < 1) {
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入订单金额"];
        return;
    }
    
    NSString *par = [NSString stringWithFormat:@"{\"user_id\" : \"%@\", \"order_id\" : \"%@\", \"name\" : \"%@\", \"money\" : \"%@\", \"type\" : \"%@\"}",[RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey],[RSA encryptString:self.orderNum.text publicKey:PublicKey],[RSA encryptString:self.orderName.text publicKey:PublicKey],[RSA encryptString:self.orderPrice.text publicKey:PublicKey],[RSA encryptString:[NSString stringWithFormat:@"%ld",(long)self.IndexType] publicKey:PublicKey]];

    [PPNetworkHelper POST:@"user/Welfare/flowcurrency" parameters:@{@"json":par} success:^(id responseObject) {

        if ([responseObject[@"code"] integerValue]==1) {
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
           
        } else {
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        }

    } failure:^(NSError *error) {
        [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
    }];
}

- (void)goToTaoOrderClick {
    
    if (self.IndexType == 2) {
        [[JDKeplerManager sharedManager] jumpToTheOrderListPageController:self];
        return;
    }
    id<AlibcTradePage> page = [AlibcTradePageFactory myOrdersPage:0 isAllOrder:YES];
    AlibcTradeTaokeParams *taoKeParams=[[AlibcTradeTaokeParams alloc] init];
    taoKeParams.pid= nil;
        //打开方式
    AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
    showParam.openType = AlibcOpenTypeH5;
    showParam.backUrl = @"tbopen25027070";
    showParam.isNeedPush = YES;
    showParam.nativeFailMode = AlibcNativeFailModeJumpH5;
    
    [[AlibcTradeSDK sharedInstance].tradeService show: self.navigationController page:page showParams:showParam taoKeParams: nil trackParam: nil tradeProcessSuccessCallback:nil tradeProcessFailedCallback:nil];
}


@end
